import { wait } from "@/utils/await";
import { message } from "antd";
import routeList from "@/utils/routeList";
import { history } from "umi";
export default {
  namespace: "user",
  state: {
    userInfo: sessionStorage.getItem("userInfo")
      ? JSON.parse(sessionStorage.getItem("userInfo") || "{}")
      : null,
  },

  effects: {
    *login({ payload }, { call, put, select }) {
      //   const { data } = yield call(queryUser, payload);
      yield wait();
      message.success("登录成功！");
      sessionStorage.setItem("Token", "xxxxxxxx");
      sessionStorage.setItem("userInfo", JSON.stringify(payload));
      sessionStorage.setItem("routeList", JSON.stringify(routeList));
      yield put({ type: "queryUserSuccess", payload });
      history.push(routeList[0].path);
    },
  },

  reducers: {
    queryUserSuccess(state, { payload }) {
      return {
        ...state,
        userInfo: payload,
      };
    },
  },
};
